/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

import * as fse from 'fs-extra';
import path from 'path';

import { BuildArtifactConst } from '../../const/build-directory-const.js';
import { CompileModeEnum } from '../../enum/compile-mode-enum.js';
import { AbstractGenerateLoaderJson } from '../abstract-generate-loader-json.js';
import { TaskNames } from '../common/task-names.js';
import { TargetTaskService } from '../service/target-task-service.js';

import { LegacyPreBuild } from './legacy-pre-build.js';

/**
 * 生成fa模型编译需要的loader.json
 */
export class LegacyGenerateHarLoaderJson extends AbstractGenerateLoaderJson {
  private readonly aceLoaderJson: string;

  declareOutputFiles() {
    return super.declareOutputFiles().addEntry(this.aceLoaderJson);
  }

  initTaskDepends() {
    this.declareDepends(LegacyPreBuild.name);
  }

  constructor(taskService: TargetTaskService) {
    super(taskService, TaskNames.LegacyFATask.GENERATE_HAR_LOADER_JSON);
    this.aceLoaderJson = path.resolve(taskService.getTargetData().getPathInfo().getIntermediatesLoaderPath(), BuildArtifactConst.LOADER_JSON);
  }

  protected doTaskAction(): void {
    const loaderObj: object = {
      modulePathMap: this.modulePathMap,
      compileMode: CompileModeEnum.JS_BUNDLE, // fa模型固定为jsBundle
      projectRootPath: this.projectRootPath,
      packageManagerType: this.isOhpmProject ? 'ohpm' : 'npm',
    };
    fse.outputJSONSync(this.aceLoaderJson, loaderObj);
  }
}
